/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef __SMBUS_H__
#define __SMBUS_H__

#include <mc/core/object.h>
#include <mc/crypto/crc32.h>
#include <mc/crypto/crc8.h>
#include <mc/engine/base.h>
#include <mc/log.h>

#include "protocol/protocol.h"
#include "std_smbus.h"

typedef struct smbus_request {
    uint16_t reserved;
    uint16_t opcode;
    uint32_t offset;
    uint32_t length;
} smbus_request_t;

constexpr uint32_t crc32_len = 4;

namespace dev {

class MC_API smbus : public std_smbus {
public:
    smbus(mc::engine::abstract_object* parent);
    ~smbus();

    void                                  init(mc::mutable_dict& params) override;
    std::pair<bool, std::vector<uint8_t>> send_and_receive(const std::string& data,
                                                           uint32_t           len) override;
    std::string construct_request_data(mc::mutable_dict& track_request) override;
    std::pair<bool, std::vector<uint8_t>>
    send_and_receive_request_in_frames(mc::mutable_dict& track_request) override;
    std::pair<bool, mc::mutable_dict> unpack_response_data(const std::string& rsp_bin,
                                                           const int32_t expect_data_len) override;
    mc::mutable_dict                  _unpack_response(const std::string& rsp_bin);

    void register_WriteRead_method(
        std::function<std::vector<uint8_t>(const std::vector<uint8_t>&, uint32_t)> func) override;
    std::string pack_smbus_request(const mc::mutable_dict& request);
    uint32_t    get_max_frame_len() override;

    // uint8_t m_buffer_len = 64;
    // uint8_t m_mcu_address = 0xD4;

private:
    std::function<std::vector<uint8_t>(const std::vector<uint8_t>&, uint32_t)> m_WriteRead_func;
};

} // namespace dev

#endif // __SMBUS_H__